#include <stdio.h>
#include <stdlib.h>

#include "reader.h"

/*
 * Sample main program for the card reader API that I wrote!
 */
int main( int argc, char *argv[] ) {

  int card_len;
  credit_card_data d;

  /* Open the port to the reader */
  int fd = get_device_port();
  if ( fd == -1 ) return 1;

  /*Read from the device.... FOREVER!!!*/
  char *card = (char*)malloc(READ_BUFFER_SIZE*sizeof(char));

  while ( 1 ) {

    card_len = read_card( fd, &card, READ_BUFFER_SIZE*sizeof(char) );

    if ( card_len == 0 ) {
      fprintf( stderr, "ERROR: Card reading failed somehow... Bailing out!\n" );
      return 1;
    }

    parse_card_as_credit_card( card, card_len, &d );
    print_credit_card( &d );

    printf( "\n" );
  }

  /*Not reached, here for example purposes*/

  /*Close the device port*/
  close_device_port( fd );

  return 0;
}
